package com.ucar.train.service.impl;

import com.ucar.train.GenerateOrderNo;
import com.ucar.train.dao.CarMapper;
import com.ucar.train.dao.OrderDetailMapper;
import com.ucar.train.dao.OrderMapper;
import com.ucar.train.dto.OrderDTO;
import com.ucar.train.dto.OrderDetailDTO;
import com.ucar.train.service.OrderService;
import com.ucar.train.utils.JodaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: OrderServiceImpl
 * @Description TODO
 * @Author: XuMingZhi
 * @Create: 2019-08-14 09:46
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private StoreServiceImpl storeService;

    @Autowired
    private OrderDetailMapper orderDetailMapper;


    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @Param: [customerId]
     * @Return: java.util.List<com.ucar.train.dto.OrderDTO>
     * @Author: XuMingZhi
     * @Date: 2019/8/16 14:22
     */
    @Override
    public List<OrderDTO> SelectOrdersByCustomerId(Long customerId) {

        return orderMapper.selectByCustomerID(customerId);
    }

    /**
     * 功能描述: 根据客户id查订单详细信息
     * 〈〉
     *
     * @Param: [customerId]
     * @Return: java.util.List<com.ucar.train.dto.OrderDTO>
     * @Author: XuMingZhi
     * @Date: 2019/8/16 14:22
     */
    @Override
    public List<OrderDetailDTO> SelectOrdersDetailByCustomerId(Long customerId) {
        List<OrderDTO> orderDTOS = orderMapper.selectByCustomerID(customerId);
        List<OrderDetailDTO> orderDetailDTOList = new ArrayList<>();
        for (OrderDTO order : orderDTOS) {
            OrderDetailDTO orderDetailDTO = orderDetailMapper.selectByOrderNo(order.getOrderNo());
            orderDetailDTOList.add(orderDetailDTO);
        }
        return orderDetailDTOList;
    }

    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @Param: [orderDTO]
     * @Return: int
     * @Author: XuMingZhi
     * @Date: 2019/8/16 14:23
     */
    @Override
    public int updateOrderDetailByOrderSelective(OrderDTO orderDTO) {
        return 0;
    }

    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @Param: []
     * @Return: java.util.List<com.ucar.train.dto.OrderDTO>
     * @Author: LiuHao
     * @Date: 2019/8/16 14:23
     */
    @Override
    public List<OrderDTO> selectAll() {
        return orderMapper.selectAll();
    }

    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @Param: [orderNo]
     * @Return: com.ucar.train.dto.OrderDTO
     * @Author: XuMingZhi
     * @Date: 2019/8/16 14:23
     */
    @Override
    public OrderDTO selectByOrderNo(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }

    /**
     * @param null
     * @return
     * @author zjm
     * @date
     */
    @Override
    public Boolean addOrder(OrderDTO orderDTO, Integer orderNum, String carType, BigDecimal cost) {
        //1.order_no
        String orderNo = GenerateOrderNo.generate(orderMapper.selectLastOrderNo());
        orderDTO.setOrderNo(orderNo);
        //2.cost,定金1000加时长*费用
        BigDecimal pay = new BigDecimal("1000");
        if (orderDTO.getRent().equals("日租")) {
            if (cost == null) {
                cost = new BigDecimal("200");
                pay = pay.add(cost);
            }

        }
        if (orderDTO.getRent().equals("月租")) {
            if (cost == null) {
                cost = new BigDecimal("200");
                pay = pay.add(cost.multiply(BigDecimal.valueOf(30)));
            }

        }
        orderDTO.setCost(pay);
        //3.create_time
        orderDTO.setCreateTime(JodaUtils.nowAsDate());
        //4.modify_time
        orderDTO.setModifyTime(JodaUtils.nowAsDate());
//        System.out.println(orderDTO);
        Boolean successful = addOrderDetail(orderNo, orderNum, carType, orderDTO.getOrderNo(), orderDTO.getStoreCode());
        if (successful) {

            orderMapper.insert(orderDTO);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean addOrderDetail(String orderNo, Integer orderNum, String carType, String OrderNo, String storeCode) {
        List<OrderDetailDTO> orderDetailDTOS = new ArrayList<>();
        //1.本地调度
        System.out.println(carType);
        System.out.println(orderNum);
        List<String> carIds = carMapper.selectCarIdbyType(carType, orderNum);
        List<String> schedulCarIds = new ArrayList<>();
//        System.out.println(carIds.toString());
        //2.判断是否足够
        if (carIds.size() < orderNum) {
            //3.调度算法,获取车辆
            schedulCarIds = storeService.schedulCarByStoreId(storeCode, carType, orderNum - carIds.size());
            if (schedulCarIds == null) {
                return false;
            }
            OrderDetailDTO orderDetailDTO = new OrderDetailDTO();
            for (int i = 0; i < orderNum; i++) {
                orderDetailDTO.setOrdNo(orderNo);
                orderDetailDTO.setGetStoreCode(storeCode);
                //门店车辆分配完
                if (i >= carIds.size()) {
                    orderDetailDTO.setCarId(schedulCarIds.get(orderNum - i - 1));
                    orderDetailDTOS.add(orderDetailDTO);
                } else {
                    orderDetailDTO.setCarId(carIds.get(i));
                    orderDetailDTOS.add(orderDetailDTO);
                }
            }
        } else {
            OrderDetailDTO orderDetailDTO = new OrderDetailDTO();
            for (int i = 0; i < orderNum; i++) {
                orderDetailDTO.setOrdNo(orderNo);
                orderDetailDTO.setCarId(carIds.get(i));
                orderDetailDTO.setGetStoreCode(storeCode);
//                System.out.println(orderDetailDTO);
                orderDetailDTOS.add(orderDetailDTO);
            }
        }
        orderDetailMapper.insertSelectiveMore(orderDetailDTOS);

        return true;
    }
}
